package com.javasm.mb_store_api_final.giftsandgoods.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.common.Parameter;
import com.javasm.mb_store_api_final.giftsandgoods.goods.dao.GgGoodsDao;
import com.javasm.mb_store_api_final.giftsandgoods.goods.entity.GgGoods;
import com.javasm.mb_store_api_final.giftsandgoods.goods.service.GgGoodsService;
import com.javasm.mb_store_api_final.giftsandgoods.goods.vo.GoodsVo;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.entity.GgGoodsType;
import com.javasm.mb_store_api_final.giftsandgoods.goodsType.service.GgGoodsTypeService;
import com.javasm.mb_store_api_final.giftsandgoods.product.entity.GgProduct;
import com.javasm.mb_store_api_final.giftsandgoods.product.service.GgProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品管理-商品管理(GgGoods)表服务实现类
 *
 * @author makejava
 * @since 2024-07-29 21:20:18
 */
@Service("ggGoodsService")
public class GgGoodsServiceImpl extends ServiceImpl<GgGoodsDao, GgGoods> implements GgGoodsService {

    @Resource
    GgGoodsTypeService goodsTypeService;
    @Resource
    GgProductService productService;
//    @Resource
//    GgBrandService brandService;



    @Override
    public IPage<GgGoods> listWithTypeName(GoodsVo goodsVo) {
        //先查询出所有的 动态列表
        LambdaQueryWrapper<GgGoods> queryWrapper=new LambdaQueryWrapper<>();


       if (goodsVo.getGmodel()!=null) {
            queryWrapper.like(GgGoods::getGmodel,goodsVo.getGmodel());
        }
       if (goodsVo.getGname()!=null) {
            queryWrapper.like(GgGoods::getGname,goodsVo.getGname());
        }
        if (goodsVo.getGcolor()!=null&&!goodsVo.getGcolor().equals("")) {
            queryWrapper.like(GgGoods::getGcolor,goodsVo.getGcolor());
        }
        if (goodsVo.getGbrand()!=null&&!goodsVo.getGbrand().equals("")) {
            queryWrapper.like(GgGoods::getGbrand,goodsVo.getGbrand());
        }

        //根据voTypeName查询出typeName对应的id
        String voTypeName = goodsVo.getTypeName();
        if (voTypeName!=null&&!goodsVo.getTypeName().equals("")) {
            GgGoodsType goodsType = goodsTypeService.getByTypeName(voTypeName);
            Integer typeId = goodsType.getId();
            queryWrapper.like(GgGoods::getGtype,typeId);
        }

        //分页
        IPage<GgGoods> page = new Page<>(goodsVo.getPageNum(),goodsVo.getPageSize());
        page(page,queryWrapper);
        //List<GgGoods> list= page.getRecords();
//        System.out.println();
//        List<GgGoods> list = list();
        page.getRecords().stream().peek(ggGoods -> {

            Integer gtypeID = ggGoods.getGtype();
            //Parameter.check(gtypeID);
            GgGoodsType goodsType = goodsTypeService.getById(gtypeID);
            String typeName = goodsType.getTypeName();
            ggGoods.setTypeName(typeName);
            //String typeId = goodsType.getId();
            //List<GgBrand> ggBrandList=brandService.getByTypeId(typeId);
        }).collect(Collectors.toList());
        return page;
    }
    /**
     *
     * 基础库-采购模式:gg_product
     * 基础库-铺货:
     * 基础库-备机:
     * 商品类别:classify
     *是否可采购:
     */
    @Override
    public GgGoods getByIdWithAnotherProperties(String gid) {
        GgGoods ggGoods = getById(gid);
        Integer gtypeID = ggGoods.getGtype();
        GgGoodsType goodsType = goodsTypeService.getById(gtypeID);
        String typeName = goodsType.getTypeName();
        ggGoods.setTypeName(typeName);
        GgProduct product = productService.getByCoding(ggGoods.getGcode());
        ggGoods.setClassify(product.getClassify());
        ggGoods.setDistribution(product.getDistribution());
        ggGoods.setPurchasingPattern(product.getPurchasingPattern());
        ggGoods.setStandby(product.getStandby());
        return ggGoods;
    }

    @Override
    public boolean updateByIdWithTypeName(GgGoods goods) {
        LambdaUpdateWrapper<GgGoods> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(GgGoods::getId,goods.getId());
        update(goods,updateWrapper);
        if (goods.getGtype()!=null) {
            Boolean b = goodsTypeService.updateTypeNameById(goods);
            return b;
        }

        return false;
    }

    @Override
    public boolean updateByIdWithClassify(GgGoods goods) {
        LambdaUpdateWrapper<GgGoods> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(GgGoods::getId,goods.getId());
        update(goods,updateWrapper);
        System.out.println(goods.getGstatus());
        Boolean b= productService.updateClassifyByCoding(goods);
        return b;
    }

    @Override
    public boolean saveGoodsWithProduct(GgGoods goods) {
        if(goods != null){
            String typeName = goods.getTypeName();
            if (typeName!=null&&!typeName.equals("")) {
                GgGoodsType goodsType = goodsTypeService.getByTypeName(typeName);
                Integer typeId = goodsType.getId();
                goods.setGtype(typeId);
            }
        }
        return save(goods);
    }


}

